﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASPNETDBTableAdapters;
using System.Data.SqlClient;

public partial class Administration_content_add : System.Web.UI.Page
{
    private NavigationTableAdapter navAdapter = new NavigationTableAdapter();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            String idParam = Request.QueryString["id"];
            if (idParam != null)
            {
                int id = int.Parse(idParam);
                ASPNETDB.NavigationDataTable items = navAdapter.GetNavigationById(id);
                if (items.Rows.Count != 0)
                {
                    ASPNETDB.NavigationRow item = (ASPNETDB.NavigationRow)items.Rows[0];
                    NavigationFormName.Text = item["navigationName"].ToString().Trim();
                    NavigationFormId.Value = item["navigationID"].ToString();
                    NavigationFormPosition.Value = item["navigationPosition"].ToString();

                    InsertButton.Visible = false;
                    UpdateButton.Visible = true;
                }
                else
                {
                    Session.Add("ErrorMessage", "Item not found");
                    Response.Redirect("/CMSProject/Administration/error.aspx", true);
                }
            }
            else
            {
                int newPosition = 0;
                if (navAdapter.GetHighestNavigationPosition() != null)
                    newPosition = (int)navAdapter.GetHighestNavigationPosition() + 1;

                NavigationFormPosition.Value = newPosition.ToString();
                InsertButton.Visible = true;
                UpdateButton.Visible = false;
            }
        }

    }
    protected void Insert(object sender, EventArgs e)
    {
        int ret = navAdapter.InsertNavigationItem(NavigationFormName.Text, 0);
        Response.Redirect("index.aspx", true);
    }

    protected void Update(object sender, EventArgs e)
    {
        String name = NavigationFormName.Text;

        int position = int.Parse(NavigationFormPosition.Value);
        int id = int.Parse(NavigationFormId.Value);

        navAdapter.UpdateNavigationItem(name, position, id);
        Response.Redirect("index.aspx", true);
    }
}